package com.devsoft.foodfrenzy;

import android.content.Context;
import android.content.SharedPreferences;

public class UserData {
	
	private static UserData INSTANCE;
	private static final String PREFS_NAME = "FoodFrenzy_UserData";
	private static final String CLASSIC_KEY = "Classic_HighScore";
	private static final String ARCADE_KEY = " Arcade_HighScore";
	private static final String LAST_KEY = " lastKey";
	private static final String SOUND_KEY = "soundKey";
	private static final String MUSIC_KEY = "musicKey";
	private static final String CONTROLS_KEY = "controlKey";
	private static final String SKIN_KEY = "skinKey";
	private static final String UNLOCK_KEY = "unlockscodeKey";
	private static final String COUNT_KEY = "unlockscountKey";
	private static final String UL1_KEY = "counter1Key";
	private static final String UL2_KEY = "counter2Key";
	private static final String UL3_KEY = "counter3Key";
	private static final String UL4_KEY = "counter4Key";
	private static final String UL5_KEY = "counter5Key";
	private static final String UL6_KEY = "counter6Key";
	private static final String UL7_KEY = "counter7Key";
	private static final String FLAG1_KEY = "newhighscoreKey";
	private static final String FLAG2_KEY = "newachievementunlockedKey";
	private SharedPreferences mSettings;
	private SharedPreferences.Editor mEditor;
	private int classic_high_score;
	private int arcade_high_score;
	private int last_score;
	private boolean sound;
	private boolean music;
	private boolean controls;
	private int skin;
	private String unlock_state;
	private int ul_classic_games;
	private int ul_arcade_games;
	private int ul_multi_games;
	private int ul_flip_count;
	private int ul_cook_count;
	private int ul_combo_chain;
	private int ul_multi_wins;
	private boolean flag_nhs;
	private boolean flag_nau;
	private int achievement_count;
	
	public UserData(){}
	
	public synchronized static UserData getInstance() {
		if(INSTANCE == null){
			INSTANCE = new UserData();
		}
		return INSTANCE;
	}
	
	public synchronized void init(Context pContext) {
		if (mSettings == null) {
			mSettings = pContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
			mEditor = mSettings.edit();
			classic_high_score = mSettings.getInt(CLASSIC_KEY, 0);
			arcade_high_score = mSettings.getInt(ARCADE_KEY, 0);
			last_score = mSettings.getInt(LAST_KEY, 0);
			sound = mSettings.getBoolean(SOUND_KEY, false);
			music = mSettings.getBoolean(MUSIC_KEY, false);
			controls = mSettings.getBoolean(CONTROLS_KEY, true);
			skin = mSettings.getInt(SKIN_KEY, 0);
			flag_nhs = mSettings.getBoolean(FLAG1_KEY, false);
			flag_nau = mSettings.getBoolean(FLAG2_KEY, false);
			/*
			 * Achievement Code Key:
			 * 0: Play 5 games in Classic Mode
			 * 1: Play 10 games in Classic Mode
			 * 2: Play 20 games in Classic Mode
			 * 3: Score 100 points in Classic Mode
			 * 4: Score 500 points in Classic Mode
			 * 5: Score 1000 points in Classic Mode
			 * 6: Play 5 games in Arcade Mode
			 * 7: Play 10 games in Arcade Mode
			 * 8: Play 20 games in Arcade Mode
			 * 9: Score 100 points in Arcade Mode
			 * 10: Score 500 points in Arcade Mode
			 * 11: Score 1000 points in Arcade Mode
			 * 12: Play 50 games in total
			 * 13: Play 100 games in total
			 * 14: Play 200 games in total
			 * 15: Flip 1000 foods
			 * 16: Flip 2000 foods
			 * 17: Flip 5000 foods
			 * 18: Cook 1000 foods
			 * 19: Cook 2000 foods
			 * 20: Cook 5000 foods
			 * 21: Get 50 combo chain
			 * 22: Get 100 combo chain
			 * 23: Get 200 combo chain
			 * 24: Play 5 games in Multiplayer Mode
			 * 25: Play 10 games in Multiplayer Mode
			 * 26: Play 20 games in Multiplayer Mode
			 * 27: Win 5 games in Multiplayer Mode
			 * 28: Win 10 games in Multiplayer Mode
			 * 29: Win 20 games in Multiplayer Mode
			 */
			unlock_state = mSettings.getString(UNLOCK_KEY, "000000000000000000000000000000");
			achievement_count = mSettings.getInt(COUNT_KEY, 0);
			ul_classic_games = mSettings.getInt(UL1_KEY, 0);
			ul_arcade_games = mSettings.getInt(UL2_KEY, 0);
			ul_multi_games = mSettings.getInt(UL3_KEY, 0);
			ul_flip_count = mSettings.getInt(UL4_KEY, 0);
			ul_cook_count = mSettings.getInt(UL5_KEY, 0);
			ul_combo_chain = mSettings.getInt(UL6_KEY, 0);
			ul_multi_wins = mSettings.getInt(UL7_KEY, 0);
		}
	}

	public synchronized int getHighScore(int type) {
		if(type == 0){
			return classic_high_score;
		}
		else if(type == 1){
			return arcade_high_score;
		}
		else{
			return -1;//ERROR!
		}
	}

	public synchronized void setHighScore(int new_hs, int type) {
		if(type == 0){
			classic_high_score = new_hs;
			mEditor.putInt(CLASSIC_KEY, classic_high_score);
		}
		else if(type == 1){
			arcade_high_score = new_hs;
			mEditor.putInt(ARCADE_KEY, arcade_high_score);
		}
		mEditor.commit();
	}
	
	public synchronized void resetHighScore(){
		this.setHighScore(0, 0);
		this.setHighScore(0, 1);
	}
	
	public synchronized int getLastScore() {
		return last_score;
	}

	public synchronized void setLastScore(int ls) {
		last_score = ls;
		mEditor.putInt(LAST_KEY, last_score);
		mEditor.commit();
	}
	
	
	public synchronized boolean getMusicPref() {
		return music;
	}

	public synchronized void setMusicPref(boolean mp) {
		music = mp;
		mEditor.putBoolean(MUSIC_KEY, music);
		mEditor.commit();
	}
	
	public synchronized boolean getSoundPref() {
		return sound;
	}

	public synchronized void setSoundPref(boolean sp) {
		sound = sp;
		mEditor.putBoolean(SOUND_KEY, sound);
		mEditor.commit();
	}
	
	public synchronized boolean getControlsPref() {
		return controls;
	}

	public synchronized void setControlsPref(boolean cp) {
		controls = cp;
		mEditor.putBoolean(CONTROLS_KEY, controls);
		mEditor.commit();
	}
	
	public synchronized int getSkinPref() {
		return skin;
	}

	public synchronized void setSkinPref(int sp) {
		skin = sp;
		mEditor.putInt(SKIN_KEY, skin);
		mEditor.commit();
	}
	
	public synchronized String getUnlocksCode() {
		return unlock_state;
	}
	
	public synchronized char getUnlocksPref(int num) {
		return unlock_state.charAt(num);
	}

	public synchronized void setUnlocksPref(char up, int num) {
		StringBuffer temp = new StringBuffer(unlock_state);
		temp.deleteCharAt(num);
		temp.insert(num, up);
		unlock_state = temp.toString();
		mEditor.putString(UNLOCK_KEY, unlock_state);
		mEditor.commit();
	}
	
	public synchronized void resetUnlocksCode() {
		this.setHighScore(0, 0);
		this.setHighScore(0, 1);
		this.setUnlocksVal(0, 1);
		this.setUnlocksVal(0, 2);
		this.setUnlocksVal(0, 3);
		this.setUnlocksVal(0, 4); 
		this.setUnlocksVal(0, 5);
		this.setUnlocksVal(0, 6); 
		this.setUnlocksVal(0, 7);
		this.setCount(0);
		this.setSkinPref(0);
		unlock_state = "000000000000000000000000000000";
		mEditor.putString(UNLOCK_KEY, unlock_state);
		mEditor.commit();
	}
	
	public synchronized void cheatMode() {
		this.setHighScore(99999, 0);
		this.setHighScore(99999, 1);
		this.setUnlocksVal(99999, 1);
		this.setUnlocksVal(99999, 2);
		this.setUnlocksVal(99999, 3);
		this.setUnlocksVal(99999, 4); 
		this.setUnlocksVal(99999, 5);
		this.setUnlocksVal(99999, 6); 
		this.setUnlocksVal(99999, 7);
		this.setCount(30);
		unlock_state = "111111111111111111111111111111";
		mEditor.putString(UNLOCK_KEY, unlock_state);
		mEditor.commit();
	}
	
	public synchronized int getUnlocksVal(int num) {
		switch(num){
		case 1:
			return ul_classic_games;
		case 2:
			return ul_arcade_games;
		case 3:
			return ul_multi_games;
		case 4:
			return ul_flip_count;
		case 5:
			return ul_cook_count;
		case 6:
			return ul_combo_chain;
		case 7:
			return ul_multi_wins;
		default:
			return -1;
		}
	}
	
	public synchronized void updateAchievements()
	{
		for(int i = 0; i < 30; i++)
		{
			if(unlock_state.charAt(i) == '0')
			{
				switch(i)
				{
					case 0:
						if(ul_classic_games == 5)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 1:
						if(ul_classic_games == 10)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 2:
						if(ul_classic_games == 20)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 3:
						if(classic_high_score >= 100)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 4:
						if(classic_high_score >= 500)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 5:
						if(classic_high_score >= 1000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 6:
						if(ul_arcade_games == 5)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 7:
						if(ul_arcade_games == 10)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 8:
						if(ul_arcade_games == 20)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 9:
						if(arcade_high_score >= 100)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 10:
						if(arcade_high_score >= 500)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 11:
						if(arcade_high_score >= 1000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 12:
						if(ul_classic_games + ul_arcade_games == 50)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 13:
						if(ul_classic_games + ul_arcade_games == 100)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 14:
						if(ul_classic_games + ul_arcade_games == 200)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 15:
						if(ul_flip_count >= 1000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 16:
						if(ul_flip_count >= 2000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 17:
						if(ul_flip_count >= 5000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 18:
						if(ul_cook_count >= 1000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 19:
						if(ul_cook_count >= 2000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 20:
						if(ul_cook_count >= 5000)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 21:
						if(ul_combo_chain >= 50)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 22:
						if(ul_combo_chain >= 100)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 23:
						if(ul_combo_chain >= 200)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 24:
						if(ul_multi_games == 5)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 25:
						if(ul_multi_games == 10)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 26:
						if(ul_multi_games == 20)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 27:
						if(ul_multi_wins == 5)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 28:
						if(ul_multi_wins == 10)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
					case 29:
						if(ul_multi_wins == 20)
						{
							setUnlocksPref('1', i);
							raiseFlag(1);
						}
						break;
				}
			}
		}
		updateCount();
	}
	
	public synchronized int getCount()
	{
		return achievement_count;
	}
	
	public synchronized void setCount(int cv)
	{
		achievement_count = cv;
		mEditor.putInt(COUNT_KEY, achievement_count);
		mEditor.commit();
	}
	
	public synchronized void updateCount()
	{
		int count = 0;
		for(int i = 0; i < 30; i++)
		{
			if(unlock_state.charAt(i) == '1')
			{
				count++;
			}
		}
		achievement_count = count;
		mEditor.putInt(COUNT_KEY, achievement_count);
		mEditor.commit();
	}
	
	public synchronized void increaseUnlockVal(int index)
	{
		switch(index){
		case 1:
			ul_classic_games++;
			mEditor.putInt(UL1_KEY, ul_classic_games);
			break;
		case 2:
			ul_arcade_games++;
			mEditor.putInt(UL2_KEY, ul_arcade_games);
			break;
		case 3:
			ul_multi_games++;
			mEditor.putInt(UL3_KEY, ul_multi_games);
			break;
		case 4:
			ul_flip_count++;
			mEditor.putInt(UL4_KEY, ul_flip_count);
			break;
		case 5:
			ul_cook_count++;
			mEditor.putInt(UL5_KEY, ul_cook_count);
			break;
		case 6:
			ul_combo_chain++;
			mEditor.putInt(UL6_KEY, ul_combo_chain);
			break;
		case 7:
			ul_multi_wins++;
			mEditor.putInt(UL7_KEY, ul_multi_wins);
			break;
		}
		mEditor.commit();
	}
	
	public synchronized void setUnlocksVal(int new_val, int num) {
		switch(num){
		case 1:
			ul_classic_games = new_val;
			mEditor.putInt(UL1_KEY, ul_classic_games);
			break;
		case 2:
			ul_arcade_games = new_val;
			mEditor.putInt(UL2_KEY, ul_arcade_games);
			break;
		case 3:
			ul_multi_games = new_val;
			mEditor.putInt(UL3_KEY, ul_multi_games);
			break;
		case 4:
			ul_flip_count = new_val;
			mEditor.putInt(UL4_KEY, ul_flip_count);
			break;
		case 5:
			ul_cook_count = new_val;
			mEditor.putInt(UL5_KEY, ul_cook_count);
			break;
		case 6:
			ul_combo_chain = new_val;
			mEditor.putInt(UL6_KEY, ul_combo_chain);
			break;
		case 7:
			ul_multi_wins = new_val;
			mEditor.putInt(UL7_KEY, ul_multi_wins);
			break;
		}
		mEditor.commit();
	}
	
	public synchronized boolean checkFlag(int flag)
	{
		boolean flag_val = false;
		if (flag == 0)
		{
			flag_val = flag_nhs;
		}
		else if (flag == 1)
		{
			flag_val = flag_nau;
		}
		return flag_val;
	}
	
	public synchronized void raiseFlag(int flag) {
		if (flag == 0)
		{
			flag_nhs = true;
			mEditor.putBoolean(FLAG1_KEY, flag_nhs);
		}
		else if (flag == 1)
		{
			flag_nau = true;
			mEditor.putBoolean(FLAG2_KEY, flag_nau);
		}
		mEditor.commit();
	}
	
	public synchronized void resetFlag(int flag) {
		if (flag == 0)
		{
			flag_nhs = false;
			mEditor.putBoolean(FLAG1_KEY, flag_nhs);
		}
		else if (flag == 1)
		{
			flag_nau = false;
			mEditor.putBoolean(FLAG2_KEY, flag_nau);
		}
		mEditor.commit();
	}
}
